/**
 * 
 */
package platformer.view.animation;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
/**
 * @author Federico
 *
 */
import java.awt.image.BufferedImage;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.awt.image.ImageProducer;
import java.awt.image.RGBImageFilter;

/**
 * The Class Frame.
 * 
 * A single frame of an animation.
 */
public class Frame {

    /** The frame. */
    private BufferedImage frame;
    
    /** The frame duration. */
    private int frameDuration;

    /**
     * Instantiates a new frame.
     *
     * @param frame the frame
     * @param duration the frame duration
     */
    public Frame(BufferedImage frame, int duration) {
        this.setFrame(frame);
        this.setFrameDuration(duration);
    }

    /**
     * Gets the frame.
     *
     * @return the frame
     */
    public BufferedImage getFrame() {
        return frame;
    }

    /**
     * Sets the frame.
     *
     * @param frame the new frame
     */
    public void setFrame(BufferedImage frame) {
        this.frame = setBackgroundToTransparent(frame);
    }

    /**
     * Gets the frame duration.
     *
     * @return the frame duration
     */
    public int getFrameDuration() {
        return frameDuration;
    }

    /**
     * Sets the frame duration.
     *
     * @param duration the new frame duration
     */
    public void setFrameDuration(int duration) {
        this.frameDuration = duration;
    }
    
    /**
     * Make transparent.
     *
     * @param image the image
     * @param c1 the c1
     * @param c2 the c2
     * @return the image
     */
    private Image MakeTransparent(BufferedImage image, Color c1, Color c2) {
    	
		final int r1 = c1.getRed();
		final int g1 = c1.getGreen();
		final int b1 = c1.getBlue();
		final int r2 = c2.getRed();
		final int g2 = c2.getGreen();
		final int b2 = c2.getBlue();
		
		ImageFilter filter = new RGBImageFilter(){
			public final int filterRGB(int x, int y, int rgb){
				int r = (rgb & 0xFF0000) >> 16;
				int g = (rgb & 0xFF00) >> 8;
				int b = rgb & 0xFF;
				if (r >= r1 && r <= r2 &&
						g >= g1 && g <= g2 &&
						b >= b1 && b <= b2){
					// Set fully transparent but keep color
					return rgb & 0xFFFFFF;
				}
				return rgb;
			}
		};

		ImageProducer ip = new FilteredImageSource(image.getSource(), filter);
		return Toolkit.getDefaultToolkit().createImage(ip);
	}
    
    /**
     * Converts an Image to a buffered image.
     *
     * @param image the image
     * @param width the width
     * @param height the height
     * @return the buffered image
     */
    private BufferedImage ImageToBufferedImage(Image image, int width, int height)
	{
		BufferedImage dest = new BufferedImage(
				width, height, BufferedImage.TYPE_INT_ARGB);
		Graphics2D g2 = dest.createGraphics();
		g2.drawImage(image, 0, 0, null);
		g2.dispose();
		return dest;
	}
    
    /**
     * Sets background to transparent.
     *
     * @param image the non transparent image
     * @return A buffered image
     */
    private BufferedImage setBackgroundToTransparent( BufferedImage image ) {    	
    	Image temp = MakeTransparent( image, new Color(64,64,64), new Color(64,64,64) );    	
    	return ImageToBufferedImage( temp, temp.getWidth( null ), temp.getHeight( null ) );
    }

}
